package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.web.Entity.SubjectOneReg;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.SubjectOneRegMapper;
import com.gljx.web.service.SubjectOneRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SubjectOneRegServiceImpl extends CommonDao implements SubjectOneRegService {
	@Autowired
	private SubjectOneRegMapper subjectOneRegMapper;
	//导入activiti工具
	ActitiviUtil activitiUtil = new ActitiviUtil();
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectOneRegMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectOneRegMapper.pagemap", paramMap, null);
        return paramMap;
	}
	
	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selectFnumber(companyId);
	}

	@Override
	public int addSubjectReg(SubjectOneReg subjectOneReg) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.insertSelective(subjectOneReg);
	}

	@Override
	public int deleteByPrimaryKey(String fid) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public SubjectOneReg selectSubjectOneRegByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selectSubjectOneRegByFid(fid);
	}

	@Override
	public int updateSubjectOneRegByFid(SubjectOneReg subjectOneReg) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.updateByPrimaryKeySelective(subjectOneReg);
	}

	@Override
	public List<SubjectOneReg> selSubjectOneRegByfbizdate(String companyid,String fbizdate, String cfcartype,String name) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selSubjectOneRegByfbizdate(companyid,fbizdate, cfcartype,name);
	}

	@Override
	public List<SubjectOneReg> selSubjectOneSumEnterPersons(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selSubjectOneSumEnterPersons(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectOneReg> selSubjectOneResultPassExamRate(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selSubjectOneResultPassExamRate(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectOneReg> selSubjectOneSumEnterPersonsByMonth(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selSubjectOneSumEnterPersonsByMonth(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectOneReg> selSubjectOneResultPassExamRateByMonth(String companyId, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub
		return subjectOneRegMapper.selSubjectOneResultPassExamRateByMonth(companyId, beginTime, endTime);
	}

	@Override
	public String selectAuditPerson(User user, Object fnumber) {
		//2.查询
		String result = activitiUtil.getTaskStateByUser(
				ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid()) ? ActivitiKey.SUBJECTONEREG_KEY_GL : ActivitiKey.SUBJECTONEREG_KEY_NC,
				fnumber                  //单据
		);
		if (activitiUtil.notTask.equals(result)) {
			System.out.println("该单据没有对应流程");
			return "noTask";
		} else if (activitiUtil.FINISH.equals(result)) {
			System.out.println("流程已结束");
			return "finish";
		}//剩下两种是该任务进行中
		else if (activitiUtil.notAssign.equals(result)) {
			System.out.println("经办人不匹配");
			return "noAssign";
		} else if (activitiUtil.ISPOWER.equals(result)) {
			System.out.println("当前用户可以审核该单据");
			return "power";
		} else {
			return "failed";
		}

	}

	@Override
	public List<SubjectOneReg> selectSubjectOneReg(String companyId,String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectOneRegMapper.selectSubjectOneReg(map);
	}



}
